package com.css.fxfzypg.modules.resultupload.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.resultupload.repository.entity.ResultEntity;
import com.css.fxfzypg.modules.resultupload.vo.TaskVo;
import com.css.fxfzypg.modules.resultupload.repository.entity.PrefectureResultEntity;
import com.css.fxfzypg.modules.resultupload.service.ResultService;
import com.css.fxfzypg.util.PlatformDownloadUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/result")
@Slf4j
public class ResultController {

    @Autowired
    ResultService resultService;
    @Autowired
    PlatformDownloadUtils platformDownloadUtils;

    /**
     * 获取任务列表
     * @param request
     * @param taskVo
     * @return
     */
    @GetMapping("/getTaskList")
    public RestResponse getTaskList(HttpServletRequest request, TaskVo taskVo) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> map=resultService.getTaskList(taskVo,curPage,pageSize);
            response = RestResponse.succeed(map);
            response.setMessage("查询成功!");
        }catch (Exception e){
            String errormessage = "查询失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 上传地市级行政区预评估结果统计表
     * @param file
     * @param taskCode
     * @return
     */
    @PostMapping("/uploadPrefectureResult")
    public RestResponse uploadStatistical(@RequestParam("file") MultipartFile file, @RequestParam("taskCode")String taskCode) {
        RestResponse response = null;
        try{
            Map<String, Object> map = resultService.uploadPrefectureResult(file,taskCode);
            boolean res = ((Boolean) map.get("res")).booleanValue();;
            if(res){
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            }else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        }catch (Exception e){
            String errormessage = "上传失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 查询已导入的地市级行政区预评估结果统计表数据
     * @param taskCode
     * @return
     */
    @GetMapping("/getPrefectureResult")
    public RestResponse getPrefectureResult(String taskCode) {
        RestResponse response = null;
        try{
            List<PrefectureResultEntity> list = resultService.getPrefectureResult(taskCode);
            response = RestResponse.succeed(list);
            response.setMessage("查询成功!");
        }catch (Exception e){
            String errormessage = "查询失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 获取上传的条数
     * @param taskCode
     * @return
     */
    @GetMapping("/getPrefectureResultCount")
    public RestResponse getPrefectureResultCount(String taskCode) {
        RestResponse response = null;
        try{
            int count = resultService.getPrefectureResultCount(taskCode);
            response = RestResponse.succeed(count);
            response.setMessage("查询成功!");
        }catch (Exception e){
            String errormessage = "查询失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 上传结果表
     * @param file
     * @param taskCode
     * @return
     */
    @PostMapping("/uploadResult")
    public RestResponse uploadResult(@RequestParam("file") MultipartFile file, @RequestParam("taskCode")String taskCode) {
        RestResponse response = null;
        try{
            Map<String, Object> map = resultService.uploadResult(file,taskCode);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            if(res){
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            }else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        }catch (Exception e){
            String errormessage = "上传失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 查询已导入的预评估结果统计表数据
     * @param taskCode
     * @return
     */
    @GetMapping("/getResult")
    public RestResponse getResult(String taskCode) {
        RestResponse response = null;
        try{
            List<ResultEntity> list = resultService.getResult(taskCode);
            response = RestResponse.succeed(list);
            response.setMessage("查询成功!");
        }catch (Exception e){
            String errormessage = "查询失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 查询已导入的预评估结果统计表数据条数
     * @param taskCode
     * @return
     */
    @GetMapping("/getResultCount")
    public RestResponse getResultCount(String taskCode) {
        RestResponse response = null;
        try{
            int count = resultService.getResultCount(taskCode);
            response = RestResponse.succeed(count);
            response.setMessage("查询成功!");
        }catch (Exception e){
            String errormessage = "查询成功!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

}
